<script lang="ts" setup>
import { computed } from "vue";

defineOptions({
  name: "Tablelog",
  inheritAttrs: false,
});

import { getTableLogPage, getTableLogForm } from "@/api/system/tablelog";

import { SysTableLogPageVO, SysTableLogForm, SysTableLogQuery } from "@/api/system/tablelog/types";

import { useScreen } from "@/store/modules/screen";

const screenStore = useScreen();
const mobile = computed(() => screenStore.device);
const screenHeight = computed(() => screenStore.screenHeight);
const screenWidth = ref(500);
const itemSpan = ref(12);

function initScreen() {
  if (mobile.value === "mobile") {
    screenWidth.value = screenStore.screenWidth;
    itemSpan.value = 24;
  } else {
    screenWidth.value = 500;
    itemSpan.value = 12;
  }
}

const queryFormRef = ref(ElForm);
const roleFormRef = ref(ElForm);
const menuRef = ref(ElTree);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);
const thentotal = ref(0);
const changeId = ref(0);
const queryParams = reactive<SysTableLogQuery>({
  pageNum: 1,
  pageSize: 10,
});

const sysTableLogList = ref<SysTableLogPageVO[]>();

const queryThenParams = reactive<SysTableLogQuery>({
  pageNum: 1,
  pageSize: 5,
});
const sysTableLogThenList = ref<SysTableLogPageVO[]>();

const dialog = reactive<DialogOption>({
  visible: false,
});

const formData = reactive<SysTableLogForm>({});

/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  getTableLogPage(queryParams)
    .then(({ data }) => {
      sysTableLogList.value = data.list;
      total.value = data.total;
			loading.value = false;
    })
    .finally(() => {
    });
}
/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

function handleQueryThen() {
  loading.value = true;
  getTableLogPage(queryThenParams)
    .then(({ data }) => {
      sysTableLogThenList.value = data.list;
      thentotal.value = data.total;
			loading.value = false;
    })
    .finally(() => {
    });
}

/**
 * 打开表单弹窗
 *
 * @param row
 */
function openDialog(row: any) {
  changeId.value = row.id;
  sysTableLogThenList.value = [];
  dialog.visible = true;
  dialog.title = "操作详情";
  queryThenParams.tableName = row.tableName;
  queryThenParams.databaseName = row.databaseName;
  queryThenParams.objectId = row.objectId;
  getTableLogPage(queryThenParams)
    .then(({ data }) => {
      sysTableLogThenList.value = data.list;
      thentotal.value = data.total;
			loading.value = false;
    })
    .finally(() => {
    });
}

/**
 * 关闭弹窗
 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  roleFormRef.value.resetFields();
  roleFormRef.value.clearValidate();

  formData.id = undefined;
  formData.sort = 1;
  formData.status = 1;
}

onMounted(() => {
  initScreen();

  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input v-model="queryParams.keywords" placeholder="请输入关键字" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table ref="dataTableRef" v-loading="loading" :data="sysTableLogList" border>
        <el-table-column label="操作类型" align="center" prop="operationTypeName" />
        <el-table-column label="数据库名" align="center" prop="databaseName" />
        <el-table-column label="表名" align="center" prop="tableName" />
        <el-table-column label="对象ID" align="center" prop="objectId" />
        <el-table-column label="操作用户ID" align="center" prop="userId" />
        <el-table-column label="操作用户类型" align="center" prop="userTypeName" />
        <el-table-column label="操作者名称" align="center" prop="operator" />
        <el-table-column label="操作IP" prop="ip" align="center" />
        <el-table-column label="创建时间" prop="createTime" align="center" />
        <el-table-column fixed="right" label="操作" :width="mobile === 'mobile' ? 100 : 220">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="openDialog(scope.row)">
              <i-ep-grid />查看历史
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      :width="screenWidth"
      :fullscreen="mobile === 'mobile'"
      class="system"
      style="width: 1400px"
      draggable
      close-icon="CloseBold"
      @close="closeDialog"
    >
      <el-form ref="roleFormRef" :model="formData" label-width="100px">
        <el-table
          ref="dataTableRef"
          v-loading="loading"
          style="height: 600px"
          stripe
          :data="sysTableLogThenList"
          border
          class="col-red"
        >
          <el-table-column label="操作类型" align="center" prop="operationTypeName" />
          <el-table-column label="操作用户ID" align="center" prop="userId" />
          <el-table-column label="操作用户类型" align="center" prop="userTypeName" />
          <el-table-column label="操作数据" align="center" prop="data" width="800" />
          <el-table-column label="操作者名称" align="center" prop="operator" />
          <el-table-column label="操作IP" prop="ip" align="center" />
          <el-table-column label="创建时间" prop="createTime" align="center" />
        </el-table>

        <pagination
          v-if="thentotal > 0"
          v-model:total="thentotal"
          v-model:page="queryThenParams.pageNum"
          v-model:limit="queryThenParams.pageSize"
          @pagination="handleQueryThen"
        />
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog()">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped lang="scss">
.col-red {
  color: red !important;
}
</style>
